﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class TreeNode<T>
    where T : IComparable
{
    private T value;
    private TreeNode<T> left;
    private TreeNode<T> right;  

    public T Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    internal TreeNode<T> Left
    {
        get { return left; }
        set { left = value; }
    }

    internal TreeNode<T> Right
    {
        get { return right; }
        set { right = value; }
    }

    public TreeNode(T value)
    {
        this.value = value;
        this.left = null;
        this.right = null;
    }

    public void AddChild(T value)
    {
        if (value.CompareTo(this.value) == -1 || value.CompareTo(this.value) == 0)
        {
            if (this.left != null)
            {
                this.left.AddChild(value);
            }
            else
            {
                TreeNode<T> newNode = new TreeNode<T>(value);
                this.left = newNode;
            }
        }
        else
        {
            if (this.right != null)
            {
                this.right.AddChild(value);
            }
            else
            {
                TreeNode<T> newNode = new TreeNode<T>(value);
                this.right = newNode;
            }
        }
    }
}
